package com.cch.demo.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cch.demo.sys.domain.Dept;
import com.cch.demo.sys.service.DeptService;
import com.cch.demo.sys.util.DataGridView;
import com.cch.demo.sys.util.ResultObj;
import com.cch.demo.sys.util.TreeNode;
import com.cch.demo.sys.vo.DeptVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 华哥
 * @since 2020-03-07
 */
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Resource
    private DeptService deptServiceImpl;

    /**
     * 初始化树
     * @param deptVo
     * @return
     */
    @RequestMapping("loadDeptManagerLeftTreeJson")
    public DataGridView loadDeptManagerLeftTreeJson(DeptVo deptVo){
       List<Dept> list = deptServiceImpl.list();
       List<TreeNode> nodes = new ArrayList<>();
       for(Dept dept:list){
           Boolean open = dept.getOpen()==1?true:false;
           nodes.add(new TreeNode(dept.getId(),dept.getPid(),dept.getTitle(),open));
       }
       return  new DataGridView(nodes);
    }

    /**
     * 加载数据表格
     * @param deptVo
     * @return
     */
    @RequestMapping("loadAllDept")
    public DataGridView LoadAllDept(DeptVo deptVo){
        IPage<Dept> page = new Page<>(deptVo.getPage(),deptVo.getLimit());
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(deptVo.getTitle()),"title",deptVo.getTitle());
        queryWrapper.like(StringUtils.isNotBlank(deptVo.getAddress()),"address",deptVo.getAddress());
        queryWrapper.like(StringUtils.isNotBlank(deptVo.getRemark()),"remark",deptVo.getRemark());
        queryWrapper.eq(deptVo.getId()!=null,"id",deptVo.getId()).or().eq(deptVo.getId()!=null,"pid",deptVo.getId());
        deptServiceImpl.page(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }


    @RequestMapping("addDept")
    public ResultObj addDept(DeptVo deptVo){
        try {
            deptVo.setCreatetime(new Date());
            deptServiceImpl.save(deptVo);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 删除
     * @param deptVo
     * @return
     */
    @RequestMapping("deleteDept")
    public ResultObj deleteDept(Integer id){
        try {
            deptServiceImpl.removeById(id);

            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 判断是否有子节点
     * @param deptVo
     * @return
     */
    @RequestMapping("checkDephasChildren")
    public Map<String,Object> checkDephasChildren(DeptVo deptVo){
            Map<String,Object> map = new HashMap<>();
            QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(deptVo.getId()!=null,"pid",deptVo.getId());
            List<Dept> list = deptServiceImpl.list(queryWrapper);
            if(list.size()>0){
                map.put("value",true);
            }else{
                map.put("value",false);
            }
            return map;

    }

    @RequestMapping("updateDept")
    public ResultObj updateDept(DeptVo deptVo){
        try {
            deptServiceImpl.updateById(deptVo);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 初始化排排序码
     * @return
     */
    @RequestMapping("loadDeptMaxOrderNum")
    public Map<String,Object> loadDeptMaxOrderNum(){
        Map<String,Object> map = new HashMap<>();
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("ordernum");
        List<Dept> list = deptServiceImpl.list(queryWrapper);
        if(list.size()>0){
            map.put("value",list.get(0).getOrdernum()+1);
        }else{
            map.put("value",1);
        }
        return map;
    }
}

